//
//  YQArticleListTable.m
//  iyuanqi
//
//  Created by zhaoliang chen on 2018/10/30.
//  Copyright © 2018 czl. All rights reserved.
//

#import "YQArticleListTable.h"
#import "YQArticleListCell.h"

@interface YQArticleListTable ()
<UITableViewDelegate>

@property(nonatomic,assign)NSInteger type;
@property(nonatomic,copy)NSString* fid;
@property(nonatomic,assign)NSInteger page_l;
@property(nonatomic,strong)ZLCellDataSource* myDataSource;
@property(nonatomic,strong)NSMutableArray* arrayArticles;

@end

@implementation YQArticleListTable

- (instancetype)initWithType:(NSInteger)type fid:(NSString*)fid {
    if (self == [super init]) {
        self.type = type;
        self.fid = fid;
        self.page_l = 1;
        self.dataSource = self.myDataSource;
        self.delegate = self;
        self.hideExtraCell = YES;
        [self commonSetTableView];
        [self adjustBehavior];
        [self registerClass:YQArticleListCell.class forCellReuseIdentifier:@"YQArticleListCell"];
    }
    return self;
}

- (void)getArticleList {
    [YQWebManager getArticleList:self.fid type:self.type page_l:self.page_l page_r:20 withSuccessBlock:^(id response) {
        if ([response[@"code"]integerValue]==0) {
            [self.arrayArticles removeAllObjects];
            [self.arrayArticles addObjectsFromArray:[YQArticleObject mj_objectArrayWithKeyValuesArray:response[@"data"]]];
            [self reloadData];
        }
    } withFailureBlock:^(NSError *error) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.nextResponder action_deliverEventsWithName:@"toArticleDetail:" parameter:self.arrayArticles[indexPath.row]];
}

- (ZLCellDataSource *)myDataSource{
    if(!_myDataSource){
        _myDataSource = ({
            ZLCellDataSource * object = [[ZLCellDataSource alloc]initWithItems:self.arrayArticles cellIdentifier:@"YQArticleListCell" configureCellBlock:^(YQArticleListCell* cell, YQArticleObject* item, NSIndexPath *indexPath) {
                cell.articleObject = item;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }];
            object;
       });
    }
    return _myDataSource;
}

- (NSMutableArray *)arrayArticles{
    if(!_arrayArticles){
        _arrayArticles = ({
            NSMutableArray * object = [[NSMutableArray alloc]init];
            object;
       });
    }
    return _arrayArticles;
}
@end
